/*
 * @Descripttion: 
 * @version: 
 * @Author: yuanliutech
 * @Date: 2022-11-02 14:26:03
 * @LastEditors: yuanliutech@lwj
 * @LastEditTime: 2025-03-21 15:17:44
 */
const app = getApp()
import Toast from '@vant/weapp/toast/toast';
import Dialog from '@vant/weapp/dialog/dialog';
import {
  validateEmoji
} from '../../../../utils/validate.js'
let spaceDel = require('../behaviors/space/space-del.js')
import {
  reqSpaceList,
  reqSpaceDefence
} from '../../../../request/api'
Page({
  behaviors: [spaceDel],
  data: {
    navHeight: app.globalData.navHeight + 'px', //头部距离
    bottomHeight: app.globalData.bottomHeight + 'px', //iphone底部距离  
    titleTip: '安装点', //删除时提示
    // parkId: '', //园区id
    pid: '', //父id
    pName: '', //父名称
    dataList: [], //房间列表
    page: 1,
    searchValue: '', //输入的搜索值
    timer: '', //搜索值定时
    isNextPage: true, //是否下一页
    triggered: false, //是否下拉刷新列表
    // 更多操作开始
    showMoreActions: false, //是否展示更多操作
    listItemInfo: {}, //点击更多时列表一项信息
    // 更多操作结束
    flatType: 0
  },
  onLoad(e) {
    this.getParkIdAndPid(e)
    // this.getDataList()
  },
  onShow() {
    this.setData({
      flatType: app.globalData.flatType
    })
    this.getInitDataList()
  },
  // 获取园区id和父id
  getParkIdAndPid(e) {
    let params = JSON.parse(e.params)
    this.setData({
      // parkId: params.parkId,
      pid: params.pid,
      pName: params.pName,
      orgId: params.orgId,
    })
  },
  /**
   * @name: 获取房间列表
   * @msg: *parkId：园区id  *pid：父id  name：空间名称
   * @return {*}
   */
  async getDataList() {
    let params = {
      orgId: this.data.orgId,
      pid: this.data.pid,
      name: this.data.searchValue,
      userId: app.globalData.userId,
      roleName: app.globalData.rolename,
      tenantId: app.globalData.tenantId,
    }
    const res = await reqSpaceList(this.data.page, 10, params)
    if (res.code !== 20000) return Toast(res.message)
    let {
      rows
    } = res.data
    if (this.data.page === 1) {
      this.setData({
        dataList: rows,
        isNextPage: true
      })
    } else if (this.data.page > 1 && rows.length) {
      this.setData({
        dataList: this.data.dataList.concat(rows),
        isNextPage: true
      })
    } else if (this.data.page > 1 && !rows.length) {
      Toast('已经到底啦~')
      this.setData({
        isNextPage: false
      })
    }
  },
  // 滚动到底部触发
  lower() {
    if (!this.data.isNextPage) return
    this.setData({
      page: this.data.page + 1,
    })
    this.getDataList()
  },
  // 下拉刷新
  async onRefresh() {
    this.setData({
      dataList: [],
      page: 1,
      isNextPage: true,
      triggered: true,
    })
    await this.getDataList()
    this.setData({
      triggered: false,
    })
  },
  // 输入框搜索
  searchChange(e) {
    this.data.searchValue = validateEmoji(e.detail.replace(/\s+/g, ''))
    this.setData({
      searchValue: this.data.searchValue,
      page: 1,
      isNextPage: true
    })
    clearTimeout(this.data.timer)
    this.data.timer = setTimeout(() => {
      this.getDataList()
    }, 1000)
  },
  // 操作列表后回到最初状态
  getInitDataList() {
    this.setData({
      page: 1,
      isNextPage: true,
      dataList: []
    })
    this.getDataList()
  },
  // 点击布撤防
  changeDefence(e) {
    let {
      item
    } = e.currentTarget.dataset
    let tip = ''
    if (e.detail) {
      tip = '此操作将开启该安装点所有设备布防设置，是否继续？'
      item.isDefence = 1
    } else {
      tip = '此操作将关闭该安装点所有设备布防设置，是否继续？'
      item.isDefence = 0
    }
    Dialog.confirm({
        title: '温馨提示',
        message: tip,
      })
      .then(async () => {
        const res = await reqSpaceDefence({
          spaceId: item.id,
          isDefence: item.isDefence
        })
        if (res.code !== 20000) return Toast(res.message)
        Toast.success("设置成功")
        this.getInitDataList()
      })
      .catch(() => {});
  },
  // 点击更多操作
  clickMore(e) {
    let {
      item
    } = e.currentTarget.dataset
    this.setData({
      listItemInfo: item,
      showMoreActions: true
    })
  },
  // 点击xx
  closeMoreActions() {
    this.unShowMoreActions()
  },
  // 隐藏更多操作弹窗
  unShowMoreActions() {
    this.setData({
      showMoreActions: false
    })
  },
  // 设备组
  clickDevice() {
    let {
      listItemInfo
    } = this.data
    let item = JSON.stringify(listItemInfo)
    // console.log(listItemInfo);
    wx.navigateTo({
      url: `/pages/index-inside-page/deviceGroup/deviceGroup?item=${item}`,
    })
    this.unShowMoreActions()
  },
  // 点击去编辑
  clickEdit() {
    let {
      listItemInfo
    } = this.data
    let item = JSON.stringify(listItemInfo)
    // console.log(listItemInfo);
    // wx.navigateTo({
    //     url: `/packageA/pages/index-inside-page/room-edit/room-edit?item=${item}`,
    // })
    wx.navigateTo({
      url: `/packageA/pages/index-inside-page/room-detail/detail/detail?item=${item}`,
    })
    this.unShowMoreActions()
  },
  goRoom(e) {
    let item = JSON.stringify(e.currentTarget.dataset.item)
    wx.navigateTo({
      url: `/packageA/pages/index-inside-page/room-detail/detail/detail?item=${item}`,
    })
  },


  // 去新增房间
  goAddRoom() {
    let {
      pid,
      orgId
      // parkId
    } = this.data
    let info = {
      pid,
      orgId
      // parkId,
    }
    let params = JSON.stringify(info)
    wx.navigateTo({
      url: `/packageA/pages/index-inside-page/room-add/room-add?params=${params}`,
    })
  },
  // 点击回到首页
  backIndex() {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
})